package cn.wolfcode.car.business.web.controller.statement;

import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.StatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.util.StringUtils;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.poi.util.StringUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.swing.plaf.nimbus.State;
import java.util.Date;

/**
 * @Author WaveHi
 * @Date 14/11/2021 下午 4:25
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
@Controller
@RequestMapping("/business/statement")
public class StatementController {

    private final String prefix = "/business/statement/";

    @Autowired
    private StatementService statementService;

    @RequiresPermissions("business:statement:view")
    @RequestMapping("/listPage")
    public String listPage() {
        return prefix + "list";
    }

    @RequiresPermissions("business:statement:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<Statement> query(
            @RequestParam(value = "pageNo", required = false) Integer pageNo,
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
            StatementQuery statementQuery) {
        //Judge data
        pageNo = pageNo != null ? pageNo : 1;
        pageSize = pageSize != null ? pageSize : 3;
        boolean cusEmpty = StringUtils.isEmpty(statementQuery.getCustomerName());
        boolean phoneEmpty = StringUtils.isEmpty(statementQuery.getCustomerPhone());
        boolean licEmpty = StringUtils.isEmpty(statementQuery.getLicensePlate());
        //Correct
        if(cusEmpty) {
            statementQuery.setCustomerName(null);
        }else {
            statementQuery.setCustomerName(statementQuery.getCustomerName().trim());
        }
        if(phoneEmpty) {
            statementQuery.setCustomerPhone(null);
        }else {
            statementQuery.setCustomerPhone(statementQuery.getCustomerPhone().trim());
        }
        if(licEmpty) {
            statementQuery.setLicensePlate(null);
        }else {
            statementQuery.setLicensePlate(statementQuery.getLicensePlate().trim());
        }
        //Query
        return statementService.getPartOfStatements(statementQuery);
    }

    @RequiresPermissions("business:statement:add")
    @RequestMapping("/addPage")
    public String addPage() {
        return prefix + "add";
    }

    @RequiresPermissions("business:statement:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult add(Statement statement) {
        //Judge data
        boolean cusEmpty = StringUtils.isEmpty(statement.getCustomerName());
        boolean phoneEmpty = StringUtils.isEmpty(statement.getCustomerPhone());
        boolean licEmpty = StringUtils.isEmpty(statement.getLicensePlate());
        boolean carEmpty = StringUtils.isEmpty(statement.getCarSeries());
        boolean infoEmpty = StringUtils.isEmpty(statement.getInfo());
        boolean arrEmpty = statement.getActualArrivalTime() != null;
        //Correct data
        if(!cusEmpty && !phoneEmpty && !licEmpty && !carEmpty) {
            statement.setCustomerName(statement.getCustomerName().trim());
            statement.setCustomerPhone(statement.getCustomerPhone().trim());
            statement.setLicensePlate(statement.getLicensePlate().trim());
            statement.setCarSeries(statement.getCarSeries().trim());
            if(!infoEmpty) {
                statement.setInfo(statement.getInfo().trim());
            }else {
                statement.setInfo(null);
            }
            if(arrEmpty) {
                statement.setActualArrivalTime(new Date());
            }
            //Add
            statementService.add(statement);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @RequiresPermissions("business:statement:edit")
    @RequestMapping("/editPage")
    public String editPage(Model model, Long id) {
        model.addAttribute("statement", statementService.getStatement(id));
        return prefix + "edit";
    }

    @RequiresPermissions("business:statement:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(Statement statement) {
        //Judge data
        boolean cusEmpty = StringUtils.isEmpty(statement.getCustomerName());
        boolean phoneEmpty = StringUtils.isEmpty(statement.getCustomerPhone());
        boolean licEmpty = StringUtils.isEmpty(statement.getLicensePlate());
        boolean carEmpty = StringUtils.isEmpty(statement.getCarSeries());
        boolean infoEmpty = StringUtils.isEmpty(statement.getInfo());
        boolean arrEmpty = statement.getActualArrivalTime() != null;
        //Correct data
        if(!cusEmpty && !phoneEmpty && !licEmpty && !carEmpty) {
            statement.setCustomerName(statement.getCustomerName().trim());
            statement.setCustomerPhone(statement.getCustomerPhone().trim());
            statement.setLicensePlate(statement.getLicensePlate().trim());
            statement.setCarSeries(statement.getCarSeries().trim());
            if(!infoEmpty) {
                statement.setInfo(statement.getInfo().trim());
            }else {
                statement.setInfo(null);
            }
            if(arrEmpty) {
                statement.setActualArrivalTime(new Date());
            }
            //Update
            statementService.update(statement);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @RequiresPermissions("business:statement:remove")
    @ResponseBody
    @RequestMapping("/remove")
    public AjaxResult remove(
            @RequestParam(value = "ids", required = false) Long[] ids) {
        if(ids != null) {
            statementService.delete(ids);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

}
